package com.lzb.permanentcalendar.basic;


import com.chad.library.adapter.base.BaseProviderMultiAdapter;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class ProviderMultiAdapter<T> extends BaseProviderMultiAdapter<T> {
    public static final int TYPE_EMPTY = 0;
    public static final int TYPE_VIEW = 1;

    private int spanCount = 1;
    private boolean hasEmptyView = false;


    public ProviderMultiAdapter(){
        this(1);
    }

    public ProviderMultiAdapter(int spanCount){
        this.spanCount = spanCount;
    }

    @Override
    public void setList(@Nullable Collection<? extends T> list) {
        hasEmptyView = getItemProvider(TYPE_EMPTY) != null;
        super.setList(onDataHandle(list));
    }

    @Override
    public void setNewInstance(@Nullable List<T> list) {
        hasEmptyView = getItemProvider(TYPE_EMPTY) != null;
        if (hasEmptyView && list != null){
            list = new ArrayList<>(onDataHandle(list));
        }
        super.setNewInstance(list);
    }

    @Override
    public void addData(@Nullable Collection<? extends T> list) {
        super.addData(onDataHandle(list));
    }


    public List<T> getValidData(){
        List<T> data = super.getData();
        if (hasEmptyView){
            data = IProviderListDataProxy.getInstance().getData(data);
        }
        return data;
    }

    private Collection<? extends T> onDataHandle(@Nullable Collection<? extends T> list){
        if (hasEmptyView){
            list = IProviderListDataProxy.getInstance().setList(list,spanCount);
        }
        return list;
    }

    @Override
    protected int getItemType(@NotNull List<? extends T> list, int i) {
        if (list.get(i) == null ){
            return TYPE_EMPTY;
        }else{
            return TYPE_VIEW;
        }
    }

    /**
     * 获取实际的数据数量
     *
     * @return
     */
    public int getRealDataCount() {
        try {
            return getDefItemCount();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}

